<?php
/**
 * FastApp.
 * 8/14/24
 * @Author 深色的海 (wx:deepsea_0024)
 * @Contact telegram:deepsea159
 */

namespace App\Fastapp\Api\Service\Pay;

use App\Fastapp\Api\Service\Account\AccountService;
use App\Fastapp\Api\Service\Account\WalletService;
use App\Fastapp\Api\Service\BaseService;
use App\Fastapp\Exception\TipException;
use App\Fastapp\Model\AccountPayInfo;
use App\Fastapp\Model\AccountRecharge;
use App\Fastapp\Model\FastappPay;
use Mine\Helper\Str;

class PayService extends BaseService
{
    public function platform(): array
    {
        return ['list' => FastappPay::query()->where(['status' => 1])->get(['name', 'code', 'icon', 'config'])->map(function ($item) {
            $item->setHidden(['config']);
            $config = array_column($item->config, 'text', 'field');
            $item->symbol = empty($config['symbol']) ? [] : explode(',', $config['symbol']);
            $item->name = fastapp_lang_text($item->name);
            return $item;
        })];
    }

    public function platformWithdrawal(): array
    {
        return ['list' => FastappPay::query()->where(['is_withdrawal' => 1, 'status' => 1])->get(['name', 'code', 'icon'])->map(function ($item) {
            $item->name = fastapp_lang_text($item->name);
            return $item;
        })];
    }

    public function recharge(array $param): array
    {
        $param['user_id'] = get_uid();
        $recharge_min_amount = api_cache()->getAppSetting(get_user_cache('type', $param['user_id']))['recharge_min_amount'] ?? '0';
        if ($recharge_min_amount && bccomp($param['amount'], $recharge_min_amount, 2) == -1) throw new TipException(fastapp_t('account.recharge_min_amount', ['amount' => $recharge_min_amount]));
        $cs = '\\App\\Fastapp\\Api\\Service\\Pay\\Platform\\' . Str::studly($param['code']) . 'Service';
        $setting = api_cache()->getAppSetting(get_user_cache('type', $param['user_id']));
        if ($setting && !empty($setting['pump_recharge'])) $param['pump'] = bcmul($param['amount'], bcdiv($setting['pump_recharge'], '100', 8), 4);
        $md = AccountRecharge::query()->create($param);
        $mobile = get_user_cache('mobile', $md->user_id) ?: '131385342424';
        return $cs::pay(array_merge($param, [
            'order_id' => 'R' . $md->id,
            'subject' => fastapp_t('pay.recharge'),
            'amount' => $param['amount'],
            'user_id' => $md->user_id,
            'mobile' => $mobile,
        ]));
    }

    public function withdrawal(array $param): bool
    {
        $param['user_id'] = get_uid();
        if (!get_currency_cache($param['symbol'])) throw new TipException(fastapp_t('fastapp.invalid_currency'));
        //交易密码验证
        $res = AccountService::checkTransactionPassword($param['password'], $param['user_id']);
        if (!$res['status']) throw new TipException(fastapp_t('account.password_error'));
        //应用提现验证
        if (!ValidateWithdrawal::validate($param['user_id'])) return false;
        $payinfo = null;
        if (!ValidateWithdrawal::withdrawal($param, $payinfo)) return false;
        return WalletService::withdrawal($param);
    }

    public static function order(array $param)
    {
        $cs = '\\App\\Fastapp\\Api\\Service\\Pay\\Platform\\' . Str::studly($param['code']) . 'Service';
        return $cs::pay($param);
    }
}